package CtrLayer;

import java.util.ArrayList;

import DBLayer.DBConnection;
import DBLayer.DBSupplier;
import DBLayer.IFDBSupplier;
import ModelLayer.Supplier;

/**
 * 
 * @author Dennis
 *
 */

public class SupplierCtr {

	public SupplierCtr() {
	}

	public void insertSup(String name, String address, String country, String phoneNo, String email)
	{    
		Supplier supObj = new Supplier();
		supObj.setName(name);
		supObj.setAddress(address);
		supObj.setCountry(country);
		supObj.setPhoneNo(phoneNo);
		supObj.setEmail(email);

		try{
			DBConnection.startTransaction();
			DBSupplier dbSup = new DBSupplier();
			dbSup.insertSupplier(supObj);
			DBConnection.commitTransaction();
		}
		catch(Exception e)
		{
			DBConnection.rollbackTransaction();
		}
	}
	
	public Supplier findSup(String name)
	{
		DBSupplier dbSup = new DBSupplier();
		return dbSup.searchSupplier(name);
	}
	
	public Supplier findSup(int id)
	{
		DBSupplier dbSup = new DBSupplier();
		return dbSup.searchSupplier(id);
	}

	public int updateSup(int id, String name, String address, String country, String phoneNo, String email)
	{
		DBSupplier dbSup = new DBSupplier();
		Supplier sup = new Supplier();
		sup.setId(id);
		sup.setName(name);
		sup.setAddress(address);
		sup.setCountry(country);
		sup.setPhoneNo(phoneNo);
		sup.setEmail(email);
		return  dbSup.updateSupplier(sup);
	}

	public ArrayList<Supplier> listSuppliers()
	{
		DBSupplier dbSup = new DBSupplier();
		ArrayList<Supplier> allSup = new ArrayList<Supplier>();
		allSup = dbSup.getAllSuppliers();
		return allSup;
	}
	public ArrayList<Supplier> listSuppliers(String filter) {
		DBSupplier dbSup = new DBSupplier();
		ArrayList<Supplier> allSup = new ArrayList<Supplier>();
		allSup = dbSup.getAllSuppliers(filter);
		return allSup;
	}
	public ArrayList<String> listSupplierNames()
	{
		DBSupplier dbSup = new DBSupplier();
		ArrayList<String> allNames = new ArrayList<String>(); 
		for(Supplier s : dbSup.getAllSuppliers())
		{
			allNames.add(s.getName());
		}
		return allNames;
	}

	public void removeSup(int id) {
		DBSupplier dbSup = new DBSupplier();
		Supplier supObj = new Supplier();
		supObj = dbSup.searchSupplier(id);
		
		try{
			DBConnection.startTransaction();
			dbSup.deleteSupplier(supObj);
			DBConnection.commitTransaction();
		}
		catch(Exception e)
		{
			DBConnection.rollbackTransaction();
		}
	}
}
